//
//  MainTabBarController.m
//

#import "MainTabBarController.h"
#import "HomeViewController.h"
#import "OrderViewController.h"
#import "MineViewController.h"

#import "MyTabBar.h"

@interface MainTabBarController ()

@property (strong, nonatomic) MyTabBar *customTabBar;

@property (nonatomic) NSInteger index;

@end

@implementation MainTabBarController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    for (UIView *child in self.tabBar.subviews) {
        if ([child isKindOfClass:[UIControl class]]) {
            [child removeFromSuperview];
        }
    }
    
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    for (UIView *child in self.tabBar.subviews) {
        if ([child isKindOfClass:[UIControl class]]) {
            [child removeFromSuperview];
        }
    }
}

- (BOOL)shouldAutorotate{
    return NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tabBar.tintColor = RGB(4, 44, 8);
    self.tabBar.backgroundColor = RGB(4, 44, 8);
    
    [self.tabBar setBackgroundImage:[UIImage new]];
    [self.tabBar setShadowImage:[UIImage new]];
        
    [self removeTabarTopLine];
    
    [UITabBar appearance].translucent = NO;
    
    if (@available(iOS 15.0, *)) {
        
        UITabBarAppearance *barApp = [[UITabBarAppearance alloc]init];
        
        barApp.backgroundColor = RGB(4, 44, 8);
        barApp.backgroundImage = [UIImage new];
        barApp.shadowColor = RGB(4, 44, 8);
        barApp.shadowImage = [UIImage new];
        
        self.tabBar.scrollEdgeAppearance = barApp;
        self.tabBar.standardAppearance = barApp;
        
    }

    
    self.index = 1;
    [self setupTabbar];
    
    [self setupAllChildViewControllers];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getN:) name:@"Sss" object:nil];

    
}

- (void)getN:(NSNotification *)noti{
   
    NSDictionary *dic = noti.userInfo;
    self.index = [esString(dic[@"index"]) integerValue];
    
    [self.customTabBar removeFromSuperview];
    
    [self setupTabbar];
    
    [self.customTabBar addTabBarButtonNorImageUrl:@"icon_TabBar1" selImageUrl:@"icon_TabBar2" title:@"Home" withSel:self.index];
    [self.customTabBar addTabBarButtonNorImageUrl:@"icon_TabBar1" selImageUrl:@"icon_TabBar2" title:@"Order" withSel:self.index];
    [self.customTabBar addTabBarButtonNorImageUrl:@"icon_TabBar1" selImageUrl:@"icon_TabBar2" title:@"Person Center" withSel:self.index];
    self.selectedIndex = self.index - 1;
    
    
}

- (void)setupTabbar {

    MyTabBar *customTabBar = [[MyTabBar alloc] initWithFrame:self.tabBar.bounds];
   
    customTabBar.backgroundColor = RGB(4, 44, 8);
    
    self.tabBar.backgroundColor = RGB(4, 44, 8);
    
    CGRect mainFrame = customTabBar.frame;
    
    mainFrame.size.height = DimensionBaseIphone6(54) + kDefaultTabBarFixHeight;
    
    customTabBar.frame = CGRectMake(0, IS_IPhoneX_All ? 0 : -20, self.tabBar.frame.size.width, mainFrame.size.height);
    
    customTabBar.delegate = self;
    
    [self.tabBar addSubview:customTabBar];
    
    [customTabBar bringSubviewToFront:self.tabBar];
    
    self.customTabBar = customTabBar;
    
    
    customTabBar.clickBtnBlock = ^(NSInteger num) {
        DLog(@"=======%ld",num);
        
        
        if (num == 1 || num == 2) {
            
            if (![[CurrentUser mine] hasLogged]) {
                [[CreditCore globleCore] loginController:^{

                }];
                
                return;
            }
            
        }
       
        self.selectedIndex = num;
        
    };
    
}

- (void)setupAllChildViewControllers {
 
    HomeViewController *home = [[HomeViewController alloc] init];
    home.view.backgroundColor = RGB(4, 44, 8);
    [self setupChildViewController:home title:@"Home" imageName:@"icon_TabBar1" selectedImageName:@"icon_TabBar2"];
    
    OrderViewController *message = [[OrderViewController alloc] init];
    message.view.backgroundColor = RGB(4, 44, 8);
    [self setupChildViewController:message title:@"Order" imageName:@"icon_TabBar1" selectedImageName:@"icon_TabBar2"];
    
    MineViewController *person = [[MineViewController alloc] init];
    person.view.backgroundColor = RGB(4, 44, 8);
    [self setupChildViewController:person title:@"Person Center" imageName:@"icon_TabBar1" selectedImageName:@"icon_TabBar2"];
    
}

- (void)setupChildViewController:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName {
    
    childVc.title = title;
 
    childVc.tabBarItem.image = [UIImage imageNamed:imageName];
    
    UIImage *selectedImage = [UIImage imageNamed:selectedImageName];
    childVc.tabBarItem.selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVc.tabBarItem.selectedImage = selectedImage;
    
  
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:childVc];
    [self addChildViewController:nav];
    
   
    [self.customTabBar addTabBarButtonNorImageUrl:imageName selImageUrl:selectedImageName title:title withSel:self.index];

}



- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    return YES;
}


- (void)removeTabarTopLine {
    CGRect rect = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [self.tabBar setBackgroundImage:img];
    [self.tabBar setShadowImage:img];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
